package com.sebklebes.radar.radar;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import com.sebklebes.radar.R;
import com.sebklebes.radar.converter.RadarEntity;
import com.sebklebes.radar.converter.ResponseConverter;
import com.sebklebes.radar.dialogs.DialogActivity;
import com.sebklebes.radar.dialogs.DialogFactory;
import com.sebklebes.radar.dialogs.DialogTypes;
import com.sebklebes.radar.httpclient.HttpClient;

import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

public class RadarService extends Service {

	public static boolean isRunning;
	private LocationManager locationManager;
	public static Location cachedLocation;
	private Timer timer;
	private NotificationManager notificationManager;

	private List<RadarEntity> radars;

	private static final String TAG = RadarService.class.getName();

	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}

	@Override
	public void onCreate() {
		super.onCreate();
		isRunning = true;
		notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
		showNotification();
		timer = new Timer();
		timer.schedule(new TimerTask() {
			public void run() {
				if (RadarService.cachedLocation != null) {
					radars = HttpClient.sendLocation(RadarService.cachedLocation);
					if (radars != null && !radars.isEmpty()) {
						mHandler.post(mUpdateResults);
					}
				}
			}
		}, 2000, 5000);

		locationManager = (LocationManager) this
				.getSystemService(Context.LOCATION_SERVICE);
		locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,
				0, locationListener);
		locationManager.requestLocationUpdates(
				LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
	}

	@Override
	public void onStart(Intent intent, int startId) {
		HttpClient.clearCache();
		super.onStart(intent, startId);
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		isRunning = false;
		locationManager.removeUpdates(locationListener);
		notificationManager.cancel(R.string.app_name);
		timer.cancel();
	}

	/**
	 * Show a notification while this service is running.
	 */
	private void showNotification() {
		Notification notification = new Notification(R.drawable.radar_icon,
				null, System.currentTimeMillis());
		notification.flags = Notification.FLAG_NO_CLEAR
				| Notification.FLAG_ONGOING_EVENT;
		Intent intent = new Intent();
		intent.setComponent(new ComponentName(this, Radar.class));
		intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
				intent, 0);
		notification.setLatestEventInfo(this, getString(R.string.app_name),
				"Tap to open.", contentIntent);
		notificationManager.notify(R.string.app_name, notification);
	}

	// Define a listener that responds to location updates
	LocationListener locationListener = new LocationListener() {
		public void onLocationChanged(Location location) {
			cachedLocation = location;
		}

		public void onStatusChanged(String provider, int status, Bundle extras) {
		}

		public void onProviderEnabled(String provider) {
		}

		public void onProviderDisabled(String provider) {
		}
	};

	final Handler mHandler = new Handler();

	private static final String ALERT = "Radars detected:\n";

	final Runnable mUpdateResults = new Runnable() {
		public void run() {
			RadarService context = RadarService.this;

			Intent intent = new Intent(context, DialogActivity.class);
			intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			intent.putExtra(DialogFactory.DIALOGS, DialogTypes.RADAR_ALERT);
			intent.putExtra(DialogFactory.MESSAGE, ALERT + ResponseConverter.convertToString(radars));
			context.startActivity(intent);

			MediaPlayer mp = MediaPlayer.create(RadarService.this, R.raw.loop);
			mp.setVolume(1, 1);
			mp.start();
		}
	};
}
